package com.neusmart.tongrentang.activity;

import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.neusmart.tongrentang.F;
import com.neusmart.tongrentang.R;
import com.neusmart.tongrentang.constants.API;
import com.neusmart.tongrentang.constants.Key;
import com.neusmart.tongrentang.dataLoadRequest.DataLoadActivity;
import com.neusmart.tongrentang.model.MParam;
import com.neusmart.tongrentang.model.User;
import com.neusmart.tongrentang.result.ResultLogin;

import cn.jpush.android.api.JPushInterface;
/**
 * 登录
 * <p/>
 * roy
 */

public class LoginActivity extends DataLoadActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private EditText etPhone, etPwd;
    private CheckBox cbPasswordSwith;
    //
    private String phone, pwd;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_login;
    }

    @Override
    protected void init() {
        initView();
        setListener();
    }

    private void initView() {
        etPhone = (EditText) findViewById(R.id.login_et_phone);
        etPhone.setText(getString(Key.LAST_LOGIN_ACCOUNT, ""));//上次登录的手机号
        etPwd = (EditText) findViewById(R.id.login_et_password);
        cbPasswordSwith = (CheckBox) findViewById(R.id.login_cb_password_switch);
    }

    private void setListener() {
        int[] resIds = {R.id.login_btn_back, R.id.login_btn_register,
                R.id.login_btn_forget_pwd, R.id.login_btn_login};
        for (int id : resIds)
            findViewById(id).setOnClickListener(this);
        cbPasswordSwith.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        if (getCurrentFocus() != null)//隐藏软键盘
            hideSoftInput();
        switch (v.getId()) {
            case R.id.login_btn_back:
                onBackPressed();
                break;
            case R.id.login_btn_register://注册
                switchActivity(RegisterActivity.class, null);
                break;
            case R.id.login_btn_forget_pwd://忘记密码
                switchActivity(ForgetPwdActivity.class, null);
                break;
            case R.id.login_btn_login://登录
                checkBeforeLogin();
                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked)
            etPwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        else
            etPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }

    private void checkBeforeLogin() {
        phone = etPhone.getText().toString();
        pwd = etPwd.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            showToast("请输入手机号");
            return;
        } else if (TextUtils.isEmpty(pwd)) {
            showToast("请输入密码");
            return;
        }
        loadData(API.LOGIN, true);
    }

    @Override
    protected void initParams(MParam param) {
        switch (param.getApi()) {
            case LOGIN:
                param.addParam("phoneNum", phone);
                param.addParam("password", pwd);
                param.addParam("pushClientId", JPushInterface.getRegistrationID(this));
                break;
            default:
                break;
        }
    }

    @Override
    protected void disposeResult(API api, String response) {
        if (response == null)
            return;
        switch (api) {
            case LOGIN:
                ResultLogin resultLogin = fromJson(response, ResultLogin.class);
                showToast(resultLogin.getFriendlyMessage());
                if (!resultLogin.isSuccess())
                    return;
                User user = resultLogin.getData().getUserProfile();
                user.setToken(resultLogin.getData().getToken());
                F.login(user);
                putString(Key.LAST_LOGIN_ACCOUNT, phone);
                setResult(RESULT_OK);
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (F.isLogin())
            finish();
    }

}
